/**
 * NameBuilder.cs
 *
 * Copyright (C) 2008,  iron9light
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using System.Reflection;
using System.Text;

namespace NUnitExtension.DataDriven.AddIn
{
    public class NameBuilder
    {
        private MethodInfo _method;
        private string _baseTestName;
        private object[] _arguments;
        private string _argumentList;

        public NameBuilder(MethodInfo method, string baseTestName, object[] arguments)
        {
            _method = method;
            _baseTestName = baseTestName;
            _arguments = arguments;
        }

        public MethodInfo Method
        {
            get { return _method; }
        }

        public string BaseTestName
        {
            get { return _baseTestName; }
        }

        public object[] Arguments
        {
            get { return _arguments; }
        }

        public string TestName
        {
            get
            {
                string baseTestName = _baseTestName;

                if (baseTestName == null || baseTestName.Length == 0)
                    baseTestName = _method.Name;

                return baseTestName + GetArgumentList();
            }
        }

        public string FullTestName
        {
            get { return _method.DeclaringType.FullName + "." + TestName; }
        }

        private string GetArgumentList()
        {
            if (_argumentList == null)
                _argumentList = "(" + CreateArgumentList() + ")";

            return _argumentList;
        }

        private string CreateArgumentList()
        {
            if (_arguments == null)
                return "null";

            StringBuilder argumentListBuilder = new StringBuilder();

            for (int i = 0; i < _arguments.Length; i++)
            {
                if (i > 0)
                    argumentListBuilder.Append(", ");

                argumentListBuilder.Append(GetArgumentString(_arguments[i]));
            }

            return argumentListBuilder.ToString();
        }

        private string GetArgumentString(object argument)
        {
            if (argument == null)
                return "null";

            return argument.ToString();
        }
    }
}